package com.cqemme.messageservice.controller;

import com.cqemme.comm.enums.EnumNote;
import com.cqemme.comm.po.NoteMessage;
import com.cqemme.comm.po.Result;
import com.cqemme.comm.utils.ResultCodeMsg;
import com.cqemme.messageservice.service.NoteService;
import com.cqemme.messageservice.util.JedisUtil;
import com.cqemme.messageservice.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/note")
@Api(tags = "短信业务")
public class NoteController {

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private NoteService noteService;

    /**
     * 验证输入的验证码有效性
     *
     * @param note
     * @return
     */
    @ApiOperation(value = "验证输入的验证码有效性", notes = "根据手机号、code、type来验证{daibin}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "type", value = "短信类型", dataType = "EnumNote", required = true),
            @ApiImplicitParam(name = "code", value = "短信码", dataType = "String", required = true),
    })
    @RequestMapping(value = "/validator", method = RequestMethod.POST)
    @ResponseBody
    public Result validator(@RequestBody NoteMessage note) {
        return noteService.validatorNote(note);
    }

    /**
     * 测试--清除验证器
     *
     * @param mobile
     * @return
     */
    @ApiOperation(value = "清除验证器", notes = "根据手机号清除所有验证器{daibin}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", required = true),
    })
    @RequestMapping(value = "/clearValidator", method = RequestMethod.GET)
    @ResponseBody
    public Result clearValidator(@RequestParam(value = "mobile") String mobile) {
        EnumNote[] eAll= EnumNote.values();
        for (EnumNote e : eAll){
            try {
                String mStr=e.getTemplateCode()+mobile;
                jedisUtil.del(mStr+"num");
                jedisUtil.del(mStr);
            } catch (Exception ex) {
            }
        }
        Result r = new Result();
        r.setStatusCode(ResultCodeMsg.CODE_200);
        return r;
    }

    /**
     * 根据手机号和类型查今天发送的次数
     *
     * @param noteMap
     * @return
     */
    @ApiOperation(value = "参数是发短信那个类", notes = "daibin")
    @RequestMapping(value = "/selectSendCount", method = RequestMethod.POST)
    @ResponseBody
    public Result selectSendCount(@RequestBody NoteMessage noteMap) {
        Result r = new Result();
        int num = 0;
        try {
            num = Integer.parseInt(jedisUtil.get(noteMap.getType().getTemplateCode() + noteMap.getMobile() + "num"));
        } catch (Exception e) {
        }
        r.setData(num);
        return r;
    }

}
